package com.tokencloud.provider.config;

import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

/**
 * publisher return 回调
 */
@Configuration
public class MqConfirmConfig  implements ApplicationContextAware {
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
       RabbitTemplate rabbitTemplate =  applicationContext.getBean(RabbitTemplate.class);
       rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {

           @Override
           public void returnedMessage(ReturnedMessage returnedMessage) {
               System.out.println("收到消息的 return callback:"
                       + " message: " + returnedMessage.getMessage()
                       + " exchange: " + returnedMessage.getExchange()
                       + " routingKey: " + returnedMessage.getRoutingKey()
                       + " replyText: " + returnedMessage.getReplyText()
                       + " replyCode: " + returnedMessage.getReplyCode()
               );

           }
       });
    }
}
